package com.xywl.escort.view.bandcard

import android.os.Bundle
import android.os.CountDownTimer
import android.text.TextUtils
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.trello.rxlifecycle2.LifecycleTransformer
import com.xywl.escort.R
import com.xywl.escort.base.BaseActivity
import com.xywl.escort.bean.AddUserBankCardReq
import com.xywl.escort.bean.BaseResp
import com.xywl.escort.bean.GetUserBankCardTokenReq
import com.xywl.escort.bean.MessageEvent
import com.xywl.escort.common.Constant.EventMessage.ADD_BAND_CARD_SUCCESS
import com.xywl.escort.common.Constant.EventType.EVENT_ADD_BAND_CARD_TYPE
import com.xywl.escort.utils.RegexUtils
import com.xywl.escort.utils.ToastUtil
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_band_card_third_step.*
import org.greenrobot.eventbus.EventBus
import timber.log.Timber
import javax.inject.Inject

@Route(path = "/xy/band/card/third")
class BandCardThirdStepActivity : BaseActivity(), BandCardThirdStepContract.View {
    var isCountDown: Boolean = false
    @Inject
    lateinit var mPresenter: BandCardThirdStepContract.Presenter

    @Autowired
    @JvmField
    var phone = ""

    @Autowired
    @JvmField
    var idCard = ""

    @Autowired
    @JvmField
    var bandCardNo = ""

    @Autowired
    @JvmField
    var bandAccountName = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_band_card_third_step)
        ARouter.getInstance().inject(this)

        showBackButton()
        setTitleText("添加银行卡")

        mPresenter.takeView(this)
        isCountDown = true
        btn_get_sms.isEnabled = false
        mVerificationTimer.start()
        btn_get_sms.setOnClickListener {
            if (!isCountDown && RegexUtils.checkMobile(phone)) {
                getCode()
            }
        }

        btn_confirm.setOnClickListener {
            submit()
        }
    }

    private fun getCode() {
        val req = GetUserBankCardTokenReq()
        req.bankAccountName = bandAccountName
        req.certificatesType = "01"
        req.certificatesNumber = idCard
        req.bankCardNo = bandCardNo
        req.phoneNum = phone
        mPresenter.getUserBankCardToken(req)
    }

    private fun submit() {
        val code = edit_sms_code.text.toString().trim()
        if (checkCode(code)) {
            val req = AddUserBankCardReq()
            req.bankAccountName = bandAccountName
            req.certificatesType = "01"
            req.certificatesNumber = idCard
            req.bankCardNo = bandCardNo
            req.phoneNum = phone
            req.smsCode = code
            mPresenter.addUserBankCard(req)
        }
    }

    private var mVerificationTimer = object : CountDownTimer((60 * 1000).toLong(), 1000) {
        override fun onTick(millisUntilFinished: Long) {
            btn_get_sms.isEnabled = false
            btn_get_sms.text = getString(R.string.account_auth_cd_format, millisUntilFinished / 1000)
        }

        override fun onFinish() {
            btn_get_sms.isEnabled = true
            btn_get_sms.text = getString(R.string.account_auth_re_get)
            isCountDown = false
        }
    }

    private fun checkCode(smsCode: String): Boolean {
        if (TextUtils.isEmpty(smsCode)) {
            ToastUtil.showTip(this, R.string.user_sms_code_empty)
            return false
        }
        return true
    }

    override fun isActive(): Boolean {
        return false
    }

    override fun <T : Any?> bindToLife(): LifecycleTransformer<T> {
        return this.bindToLifecycle()
    }

    override fun getUserBankCardTokenSuccess(resp: BaseResp?) {
        Timber.e("resp=$resp")
        isCountDown = true
        btn_get_sms.isEnabled = false
        mVerificationTimer.start()
    }

    override fun getUserBankCardTokenFailure(msg: String?) {
        ToastUtil.showTip(this, msg)
    }

    override fun addUserBankCardSuccess(resp: BaseResp?) {
        ToastUtil.showTip(this, "添加银行卡成功")

        EventBus.getDefault().post(MessageEvent(EVENT_ADD_BAND_CARD_TYPE, ADD_BAND_CARD_SUCCESS))
        finish()
    }

    override fun addUserBankCardFailure(msg: String?) {
        ToastUtil.showTip(this, msg)
    }
}
